package com.ktjiaoyu.news.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.news.entity.Patient;
import com.ktjiaoyu.news.service.PatinetService;
import com.ktjiaoyu.news.service.impl.PatinetServiceImpl;
import com.ktjiaoyu.news.utils.Page;
import com.ktjiaoyu.news.utils.R;
import com.ktjiaoyu.news.utils.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserServlet", urlPatterns = "/api/admin/newsuser")
public class NewsUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        PatinetService patinetService = new PatinetServiceImpl();
        //根据这个判断执行那个方法
        String opr = request.getParameter("opr");
        //用户姓名和手机号
        String patientName = request.getParameter("patientName");
        String phoneNum = request.getParameter("phoneNum");
        //创建用户对象
        Patient patient = new Patient();
        patient.setPatientName(patientName);
        patient.setPhoneNum(phoneNum);
        // 返回的json对象
        R result = null;
        if ("page".equals(opr)) {
            // 分页查询
            Integer pageNo = RequestUtil.parseInt(request, "pageNo", 1);
            Integer pageSize = RequestUtil.parseInt(request, "pageSize", 10);
            Page<Patient> page = patinetService.findPage(patient, pageNo, pageSize);
            // 返回分页数据
            result = R.ok(page);
        }
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}
